<?php
namespace Apipad\Controller;
use Think\Controller;
//面料设计
header("content-type:application/json;charset=utf-8");
class MianliaoController extends CommonController{
    //默认面料列表
    public function mianliao_cate_list(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $model = M('mianliao_cate');
        $where['isdel']=0;
        $where['uid']=UID;
        if($keyword) {
            $where['title'] = array('like',"%".$keyword."%");
        }
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,title')->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
    public function mianliao_cate(){
        $model = M('mianliao_cate');
        $where['isdel']=0;
        $where['uid']=UID;
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,title')->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
    public function goods_list(){
        $model = M('user_goods');
        $where['isdel']=0;
        $where['uid']=UID;
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,goods_name')->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
    public function mianliao_list(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $cate_id = $detail['cate_id']? : 0;
        $model = M('mianliao');
        $where['isdel']=0;
        $where['uid']=UID;
        if($keyword) {
            $where['mianliao_name'] = array('like',"%".$keyword."%");
        }
        if($cate_id) {
            $where['cate_id'] = $cate_id;
        }

        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,cate_id,mianliao_name,mianliao_pic,suppler_price')->order('id desc')->page($page, $limit)->select();
        if($list){
             foreach($list as $k=>$val){
                     $list[$k]['cate_name']=M('mianliao_cate')->where(['id'=>$val['cate_id']])->getField('title');
             }
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }

    public function mianliao_cate_detail(){
        $detail = I('post.');
        $id=$detail['id'];
        $model = M('mianliao_cate');
        $where['id']=$id;
        $where['isdel']=0;
        $where['uid']=UID;
        $count=$model->where($where)->count();
        if($count==0){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        $info= $model->where($where)->find();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']=$info;
        exit(json_encode($data_cc));
    }
    public function mianliao_detail(){
        $detail = I('post.');
        $id=$detail['id'];
        $model = M('mianliao');
        $where['id']=$id;
        $where['uid']=UID;
        $where['isdel']=0;
        $count=$model->where($where)->count();
        if($count==0){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        $info= $model->where($where)->find();
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']=$info;
        exit(json_encode($data_cc));
    }
    public function addcate(){
        $detail = I('post.');
        $title=$detail['title'];
        $id= $detail['id']? : 0;
        $data=[];
        $data['title']=$title;
        $data['uid']=UID;
        $model = M('mianliao_cate');
        if(empty($title)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入分类名称";
            exit(json_encode($data_cc));
        }
        if($id){
            $resault=$model->where(['id'=>$id])->save($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "编辑成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "编辑失败";
            }
            exit(json_encode($data_cc));
        }else{
            $resault=$model->add($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "添加成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "添加失败";
            }
            exit(json_encode($data_cc));
        }
    }
    public function cate_del(){
        $detail = I('post.');
        $model = M('mianliao_cate');
        $id= $detail['id']? : 0;
        $info=$model->where(['id'=>$id,'isdel'=>0])->find();
        if(empty($info)){
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='信息查询失败';
            exit(json_encode($data_cc));    
        }
        $data_cc=[];
        $result=$model->where(['id'=>$id,'isdel'=>0])->save(['isdel'=>1]);
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
    }


    public function add_mianliao(){
        $detail = I('post.');
        $user_goods_model = M('user_goods');
        $mianliao_name=$detail['mianliao_name'];
        $id= $detail['id']? : 0;
        $mianliao_name= $detail['mianliao_name']? :'';
        $cate_id= $detail['cate_id']? : 0;
        $goods_id= $detail['goods_id']? : 0;
        $suppler_price= 0;
        $mianliao_pic= $detail['mianliao_pic']? : '';
        $data=[];
        $data['mianliao_name']=$mianliao_name;
        $data['cate_id']=$cate_id;
        $data['goods_id']=$goods_id;
        if($goods_id){
             $suppler_price=$user_goods_model->where(['uid'=>UID,'isdel'=>0,'id'=>$goods_id])->getField('goods_price');
             if(empty($suppler_price)){
                $data_cc['code'] = 0;
                $data_cc['msg'] = "该商品不是你的吧";
                exit(json_encode($data_cc));
             }
        }
        $data['suppler_price']=$suppler_price;
        $data['mianliao_pic']=$mianliao_pic;
        $data['uid']=UID;
        $model = M('mianliao');
        if(empty($mianliao_name)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入面料名称";
            exit(json_encode($data_cc));
        }
        if(empty($mianliao_name)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入面料名称";
            exit(json_encode($data_cc));
        }
        if(empty($cate_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请选择分类";
            exit(json_encode($data_cc));
        }
        if(empty($mianliao_pic)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请上传面料图片";
            exit(json_encode($data_cc));
        }
        if($id){
            $resault=$model->where(['id'=>$id])->save($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "编辑成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "编辑失败";
            }
            exit(json_encode($data_cc));
        }else{
            $resault=$model->add($data);
            if($resault){
                $data_cc['code'] = 200;
                $data_cc['msg'] = "添加成功";
            }else{
                $data_cc['code'] = 0;
                $data_cc['msg'] = "添加失败";
            }
            exit(json_encode($data_cc));
        }
    }
    public function minaliao_del(){
        $detail = I('post.');
        $model = M('mianliao');
        $id= $detail['id']? : 0;
        $info=$model->where(['id'=>$id,'isdel'=>0,'uid'=>UID])->find();
        if(empty($info)){
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='信息查询失败';
            exit(json_encode($data_cc));    
        }
        $data_cc=[];
        $result=$model->where(['id'=>$id,'isdel'=>0,'uid'=>UID])->save(['isdel'=>1]);
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
    }

    
}
